/**
* 头部菜单
*/
<template>
  <div>
    <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
      <el-button class="buttonimg">
        <img class="showimg" :src="collapsed?imgsq:imgshow" @click="toggle(collapsed)">
      </el-button>
      <el-submenu index="2" class="submenu">
        <!-- <template slot="title">{{user.userRealName}}</template> -->
        <template slot="title">{{user.name}}</template>
<!--        <el-menu-item index="2-1">设置</el-menu-item>-->
        <el-menu-item @click="showPwdUpdate = true" index="2-2">密码修改</el-menu-item>
        <el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
      </el-submenu>
    </el-menu>
    <el-dialog title="密码修改" :visible.sync="showPwdUpdate" width="40%" @click='closeDialog("editPwd")'>
      <el-form label-width="90px" ref="editForm" :model="editForm">
        <el-form-item label="旧密码" prop="curPwd">
          <el-input size="small" v-model="editForm.curPwd" type="password" auto-complete="off" placeholder="请输入当前密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="nowPwd">
          <el-input size="small" v-model="editForm.nowPwd" type="password" auto-complete="off" placeholder="密码修改为"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="nowPwd1">
          <el-input size="small" v-model="editForm.nowPwd1" type="password" auto-complete="off" placeholder="请再次输入"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("editPwd")'>取消</el-button>
        <el-button size="small" type="primary" class="title" @click="doEditPwd">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { loginout, userPwd, userInfo } from '../api/userMG'
export default {
  name: 'navcon',
  data() {
    return {
      editForm:{
        nowPwd: '',
        curPwd: '',
        nowPwd1: ''
      },
      showPwdUpdate: false,
      collapsed: true,
      imgshow: require('../assets/img/show.png'),
      imgsq: require('../assets/img/sq.png'),
      user: {}
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    userInfo().then(res=>{
      this.user = res.data.user
    })
  },
  methods: {
    doEditPwd(){
      if(this.curPwd !== this.nowPwd1){
        this.$message.warning("两次密码不一致")
        return
      }
      userPwd(this.editForm.curPwd, this.editForm.nowPwd1).then(res=>{
        if(res.data.code === 0){
          // 修改成功
          this.showPwdUpdate = false
          this.$message.success("密码修改成功")
          this.doExit()
        } else {
          this.$message.error("密码修改失败")
        }
      })

    },
    closeDialog(type){
      if(type === 'editPwd'){
        this.showPwdUpdate = false
      }
    },
    doExit(){
      loginout()
        .then(res => {
          if (res.data.code === 0) {
            this.$store.commit('logout', 'false')
            this.$router.push({ path: '/login' })
            this.$message({
              type: 'success',
              message: '已退出登录!'
            })
          } else {
            this.$message.error(res.data.msg)
            this.logining = false
            return false
          }
        })
        .catch(err => {
          // 获取图形验证码
          // this.getcode()
          this.logining = false
          this.$message.error('退出失败，请稍后再试！')
        })
    },
    // 退出登录
    exit() {
      this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doExit()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype
      this.$root.Bus.$emit('toggle', this.collapsed)
    }
  }
}
</script>
<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
}
.submenu {
  float: right;
}
.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}
.showimg {
  width: 26px;
  height: 26px;
  position: absolute;
  top: 17px;
  left: 17px;
}
.showimg:active {
  border: none;
}
</style>
